import logging
from psycopg_pool import ConnectionPool


class Connection:
    def __init__(self, config):
        self.pool = None
        self.url = None
        self.pg_config = {"dbname": config["pg"]["dbname"], "user": config["pg"]["user"],
                          "password": config["pg"]["password"], "host": config["pg"]["host"],
                          "port": config["pg"]["port"], "sslmode": "disable"}

    def get_pool(self):
        self.url = " ".join(f"{key}={value}" for key, value in self.pg_config.items())
        logging.info("creating pg pool of {}".format(self.url))
        self.pool = ConnectionPool(
            self.url,
            min_size=1,
            max_size=5,
            timeout=10,
            max_idle=1,

        )
        return self.pool
